import React, { FC, useEffect } from 'react';
import { Modal, Form, InputNumber, Input, Select} from 'antd';
import { StudyContentItem } from '@/services/study/content';

interface OperationModalProps {
  open: boolean;
  studyContentItem: Partial<StudyContentItem> | undefined;
  onSubmit: (values: StudyContentItem) => void;
  onCancel: () => void;
}

const formLayout = {
  labelCol: { span: 7 },
  wrapperCol: { span: 13 },
};

const StudyContentForm: FC<OperationModalProps> = (props) => {
  const [form] = Form.useForm();
  const { open, studyContentItem, onCancel, onSubmit } = props;

  useEffect(() => {
    if (form && !open) {
      form.resetFields();
    }
  }, [props.open]);

  useEffect(() => {
    if (studyContentItem) {
      form.setFieldsValue({
        ...studyContentItem,
      });
    }
  }, [props.studyContentItem]);

  const handleSubmit = () => {
    if (!form){
      return;
    } 
    form.submit();
  };

  const handleFinish = (values: { [key: string]: any }) => {
    if (onSubmit) {
      onSubmit({ ...values } as StudyContentItem);
    }
  };

  const modalFooter = { okText: '保存', onOk: handleSubmit, onCancel };

  const renderContent = () => {
    return (
      <>
        <Form.Item
          label="类型"
          name="type"
          rules={[{ required: true, message: '请选择类型' }]}>
          <Select placeholder="请选择类型">
            <Select.Option value="E">电子书</Select.Option>
            <Select.Option value="B">书箱</Select.Option>
            <Select.Option value="V">视频</Select.Option>
          </Select>
        </Form.Item>
        <Form.Item
          label="名称"
          name="name"
          rules={[{ required: true, message: '请输入名称' }]}
        >
          <Input placeholder="请输入名称" />
        </Form.Item>
        <Form.Item
          label="作者"
          name="author"
          rules={[{ required: true, message: '请输入作者' }]}
        >
          <Input placeholder="请输入作者" />
        </Form.Item>
        <Form.Item
          label="译者"
          name="translator"
        >
          <Input placeholder="请输入译者" />
        </Form.Item>
        <Form.Item
          label="出版社"
          name="publisher"
          rules={[{ required: true, message: '请输入出版社' }]}
        >
          <Input placeholder="请输入出版社" />
        </Form.Item>
        <Form.Item
          label="来源"
          name="resource"
          rules={[{ required: true, message: '请选择来源' }]}>
          <Select placeholder="请选择来源">
            <Select.Option value="微信读书">微信读书</Select.Option>
            <Select.Option value="B站">B站</Select.Option>
            <Select.Option value="百度网盘">百度网盘</Select.Option>
            <Select.Option value="阿里云盘">阿里云盘</Select.Option>
            <Select.Option value="纸质书">纸质书</Select.Option>
          </Select>
        </Form.Item>
        <Form.Item 
          label="排序"
          name="sortNum"
          rules={[{ required: true, message: '请输入排序' }]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={1}
            max={100}
            step={1}
          />
        </Form.Item>
        <Form.Item 
          label="目标量"
          name="targetIndex"
          rules={[{ required: true, message: '请输入目标量' }]}
        >
          <InputNumber
            style={{ width: '100%' }}
            min={1}
            max={15000}
            step={10}
          />
        </Form.Item>
        <Form.Item label="备注" name="remark">
          <Input.TextArea rows={4} placeholder="请输入备注" />
        </Form.Item>
        <Form.Item hidden={true} name="id">
          <Input hidden={true} />
        </Form.Item>
        <Form.Item hidden={true} name="catalogId">
          <Input hidden={true} />
        </Form.Item>
      </>
    );
  };

  return (
    <Modal
      title={`${
        studyContentItem?.optType === 'edit'
          ? '编辑内容'
          : '添加内容'
      }`}
      width={640}
      destroyOnClose
      open={open}
      {...modalFooter}
    >
      <Form {...formLayout} form={form} onFinish={handleFinish}>
        {renderContent()}
      </Form>
    </Modal>
  );
};

export default StudyContentForm;